package com.central.apps.handler.base;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.central.app.common.base.BaseHandler;
import com.central.app.service.HandlerService;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SpringUtils;
import com.central.common.util.SysUserUtil;
import com.central.common.util.ToolUtil;
import com.central.common.web.Result;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.text.ParagraphView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class LvBaseHandler extends BaseHandler {

	@Autowired
	private HandlerService handlerService;
    /**
     * 新建主表及子表时添加默认值
     *
     * @param app
     * @return
     */
	public void addDefault(String app, String object, Map<String, Object> defaultData) throws Exception {

		/*LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
		String deptcode =loginAppUser.getDeptcode();
		String pcode =deptcode;
		List<Map<String, Object>> deptcodeList = handlerService.findBySql("select fullname from sys_dept where code = '" + deptcode + "'");
		String deptfullname =MapUtils.getString(deptcodeList.get(0), "fullname");
		String pfullname =deptfullname;
		//循环获取一二级,最后一级部门数据,对应总公司,分公司,部门
		boolean flag =true;
		List<String> deptLists =new ArrayList<>();
		deptLists.add(deptcode);
		List<String> deptnameLists =new ArrayList<>();
		deptnameLists.add(deptfullname);
		while(flag){
			List<Map<String, Object>> deptMapList = handlerService.findBySql("select pcode from sys_dept where code ='"+ pcode +"';");
			pcode = (deptMapList.size()>0 && ToolUtil.isNotEmpty(MapUtils.getString(deptMapList.get(0),"pcode")))?MapUtils.getString(deptMapList.get(0),"pcode"):"";
			List<Map<String, Object>> deptMapNameList = handlerService.findBySql("select fullname from sys_dept where code ='"+ pcode +"';");
			pfullname =(deptMapNameList.size()>0 && ToolUtil.isNotEmpty(MapUtils.getString(deptMapNameList.get(0),"fullname")))?MapUtils.getString(deptMapNameList.get(0),"fullname"):"";
			deptLists.add(pcode);
			deptnameLists.add(pfullname);
			if( pcode==null || "".equals(pcode)){
				flag =false;
			}
		}
		String sitecode="",sitefullname="" ,orgcode="" ,orgfullname="";
		if(deptLists!=null && deptLists.size()==2 ){
			orgcode = deptcode;
			orgfullname = deptfullname;
			sitecode = "";
			sitefullname = "";
			deptcode ="";
			deptfullname="";
		}else if(deptLists!=null && deptLists.size()==3 ){
			sitecode =deptcode;
			sitefullname =deptfullname;
			orgcode =deptLists.get(deptLists.size()-2);
			orgfullname =deptnameLists.get(deptnameLists.size()-2);
			deptcode ="";
			deptfullname="";
		}else{
			sitecode =deptLists.get(1);
			sitefullname =deptnameLists.get(1);
			orgcode =deptLists.get(deptLists.size()-2);
			orgfullname =deptnameLists.get(deptnameLists.size()-2);
		}*/
		//获取登录用户信息
		LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
		//部门
		String deptcode =loginAppUser.getDeptcode();
		String pcode =deptcode;
		List<Map<String, Object>> deptcodeList = handlerService.findBySql("select fullname from sys_dept where code = '" + deptcode + "'");
		String deptfullname =MapUtils.getString(deptcodeList.get(0), "fullname");
		String pfullname =deptfullname;
		//循环获取一二级,最后一级部门数据,对应总公司,分公司,部门
		boolean flag =true;
		List<String> deptLists =new ArrayList<>();
		deptLists.add(deptcode);
		List<String> deptnameLists =new ArrayList<>();
		deptnameLists.add(deptfullname);
		while(flag){
			List<Map<String, Object>> deptMapList = handlerService.findBySql("select pcode from sys_dept where code ='"+ pcode +"';");
			pcode = (deptMapList.size()>0 && ToolUtil.isNotEmpty(MapUtils.getString(deptMapList.get(0),"pcode")))?MapUtils.getString(deptMapList.get(0),"pcode"):"";
			List<Map<String, Object>> deptMapNameList = handlerService.findBySql("select fullname from sys_dept where code ='"+ pcode +"';");
			pfullname =(deptMapNameList.size()>0 && ToolUtil.isNotEmpty(MapUtils.getString(deptMapNameList.get(0),"fullname")))?MapUtils.getString(deptMapNameList.get(0),"fullname"):"";
			deptLists.add(pcode);
			deptnameLists.add(pfullname);
			if( pcode==null || "".equals(pcode)){
				flag =false;
			}
		}
		String sitecode="",sitefullname="" ,orgcode="" ,orgfullname="";
		if(deptLists!=null && deptLists.size()==2 ){
			orgcode = deptcode;
			orgfullname = deptfullname;
			sitecode = "";
			sitefullname = "";
			deptcode ="";
			deptfullname="";
		}else if(deptLists!=null && deptLists.size()==3 ){
			sitecode =deptcode;
			sitefullname =deptfullname;
			orgcode =deptLists.get(deptLists.size()-2);
			orgfullname =deptnameLists.get(deptnameLists.size()-2);
			deptcode ="";
			deptfullname="";
		}else{
			sitecode =deptLists.get(1);
			sitefullname =deptnameLists.get(1);
			orgcode =deptLists.get(deptLists.size()-2);
			orgfullname =deptnameLists.get(deptnameLists.size()-2);
		}
		defaultData.put("deptcode", deptcode);
		defaultData.put("sitecode", sitecode);
		defaultData.put("orgcode", orgcode);

	}

	public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
		//判断是否是人员表
		if ("jobplanApp".equals(app)) {
			if ("gq_joblabor".equals(objName)) {
				String jpnum = MapUtils.getString(data, "jpnum");

			}
		}
	}

	@Override
	public void afterFindById(String app, Map<String, Object> data) throws Exception {
		if("assetcheckheadApp".equals(app)){
			String checknum = MapUtils.getString(data, "checknum");
			String sql="select * from gq_assetcheckdetail where checknum ='"+checknum+"'";
			List<Map<String, Object>> bySql = handlerService.findBySql(sql);
			int total=0;//总计
			int Disked=0;//已盘
			int notDisked=0;//未盘
			if(bySql!=null && bySql.size()>0){
				total=bySql.size();
				for (Map<String, Object> map : bySql) {
					String status = MapUtils.getString(map, "status");
					if(StringUtils.isNotBlank(status)&&"已盘".equals(status)){
						Disked++;
					}else if(StringUtils.isNotBlank(status)&&"未盘".equals(status)){
						notDisked++;
					}
				}
			}
			data.put("total",total);
			data.put("Disked",Disked);
			data.put("notDisked",notDisked);
		}
	}

	@Override
	public Object operate(String app, String type, Map<String, Object> params) throws Exception {
		List<String> ids =new ArrayList<>();
		try {
			if("updateStatus".equals(type)){
				if("assetcheckheadApp".equals(app)){
					String assetnum = MapUtils.getString(params, "assetnum");
					String checknum = MapUtils.getString(params, "checknum");
					//判断主表状态是否是审核通过的
					String sql="select * from gq_assetcheckhead where checknum ='"+checknum+"'";
					List<Map<String, Object>> bySql = handlerService.findBySql(sql);
					if(bySql!=null&&bySql.size()>0){
						Map<String, Object> map = bySql.get(0);
						if("审批通过".equals(MapUtils.getString(map,"status"))){
							return Result.failed("该资产盘点已审批通过，不能进行操作！");
						}
					}
					//只能扫该资产盘点下的，不是的提示
					List<Map<String, Object>> ssetcheckdetailList = handlerService.findBySql("select * from gq_assetcheckdetail where checknum ='" + checknum + "'");
					List<String> assetnumList = new ArrayList<>();
					if(ssetcheckdetailList!=null&&ssetcheckdetailList.size()>0){
						for (Map<String, Object> map : ssetcheckdetailList) {
							String assetnum1 = MapUtils.getString(map, "assetnum");
							if(StringUtils.isNotBlank(assetnum1)){
								assetnumList.add(assetnum1);
							}
						}
					}
					if(assetnumList!=null&&assetnumList.size()>0){
						if(!assetnumList.contains(assetnum)){
							return Result.failed("该资产明细不属于该资产盘点，请重新进行扫描！");
						}
					}else{
						return Result.failed("该资产明细不属于该资产盘点，请重新进行扫描！");
					}
					handlerService.updateBySql("UPDATE gq_assetcheckdetail set status = '已盘', factquantity = '有' where assetnum = '" + assetnum + "'");
					List<Map<String, Object>> bySql1 = handlerService.findBySql("select id from gq_assetcheckdetail where assetnum = '" + assetnum + "'");
					for(Map<String,Object> map :bySql1){
						String id =MapUtils.getString(map,"id");
						ids.add(id);
					}
				}
			}
		} catch (Exception e) {
			return Result.failed("操作失败！");
		}
		return Result.succeed(ids,"操作成功");
	}
}
